import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
export default () => {
  const ruleFormRef = ref();
  const router = useRouter();
  const validuserName = (rule, value, callback) => {
    if (value === "") {
      callback(new Error("请输入用户名"));
    } else {
      callback();
    }
  };
  const validpassword = (rule, value, callback) => {
    if (value === "") {
      callback(new Error("请输入密码"));
    } else {
      callback();
    }
  };

  const ruleForm = reactive({
    userName: "",
    password: "",
    verify: "",
  });

  const rules = reactive({
    userName: [{ validator: validuserName, trigger: "blur" }],
    password: [{ validator: validpassword, trigger: "blur" }],
  });

  const submitForm = (formEl) => {
    window.localStorage.setItem("token", "1111");
    if (!formEl) return;
    formEl.validate((valid) => {
      if (valid) {
        router.push("/index/home");
      } else {
        console.log("error submit!");
        return false;
      }
    });
  };
  return {
    ruleFormRef,
    ruleForm,
    rules,
    submitForm,
  };
};
